using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;

public partial class ResMgr : Singleton<ResMgr>
{
    // 模型
    const string ui_prefabs_root = "Prefab/UIPrefabs/"; // UI预制体
    const string sound_root = "Sounds/"; // 声音
    const string csv_config = "GameConfig/"; // 游戏配置文件
    const string tool_prefabs_root = "Prefab/ToolPrefabs/"; // 工具预制体
    const string demo_player_root = "Prefab/ModlePrefabs/"; // Player 的预制体 

    private bool usingAssetBundle = false; // 默认不使用AB包

    private Dictionary<string, Object> ui_prefabs_res = new Dictionary<string, Object>();
    private Dictionary<string, Object> sound_res = new Dictionary<string, Object>();
    private Dictionary<string, Object> csv_config_res = new Dictionary<string, Object>();
    private Dictionary<string, Object> input_res = new Dictionary<string, Object>();
    private Dictionary<string, Object> tool_prefabs_res = new Dictionary<string, Object>();
    private Dictionary<string, Object> demo_player_res = new Dictionary<string, Object>();

    public GameObject load_demo_player_prefab(string name)
    {
        return load<GameObject>(demo_player_res, demo_player_root, name);
    }

    public GameObject load_tool_prefab(string name)
    {
        return load<GameObject>(tool_prefabs_res, tool_prefabs_root, name);
    }

    public GameObject load_ui_prefab(string name)
    {
        return load<GameObject>(ui_prefabs_res, ui_prefabs_root, name);
    }

    public AudioClip load_sound(string name)
    {
        return load<AudioClip>(sound_res, sound_root, name);
    }


    public TextAsset LoadConfigs(string name)
    {
        return load<TextAsset>(csv_config_res, csv_config, name);
    }



    private T load<T>(Dictionary<string, Object> res_set, string _path, string _name) where T : Object
    {
       string path = _path + _name;
        if (res_set.ContainsKey(path))
        {
            return (T)res_set[path];
        }

        if (usingAssetBundle)
        { // 以后再扩展;



        }
        else
        {
            string respath = path;
            int index = respath.LastIndexOf('.');
            if (index != -1)
            {
                respath = respath.Substring(0, index);
            }

            T obj = Resources.Load<T>(respath);// 必须放在 Resources目录，才可以
            if (obj)
            {
                res_set.Add(path, (Object)obj);
            }
            return obj;
        }
        return null;
    }
}
